package mobile.TheOldRepublic;

import java.text.SimpleDateFormat;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.app.ProgressDialog;
import android.content.Context;

import com.explodingzombie.util.RssHandler;

public class TorRssHandler extends RssHandler {

    private static SimpleDateFormat dateFromFormater = new SimpleDateFormat(
                    "EEE, d MMM yyyy HH:mm:ss Z");
    public ProgressDialog pd = null;
		
	private String idTag;
	private String linkTag;
	private String titleTag;
	private String summaryTag;
	private String contentTag;
	private String typeTag;
	private String typeValueAttr;
	private String lastUpdateTag;
	private String authorTag;
	private String thumbnailTag;
	private String thumbnailDataTypeAttr;
	private String thumbnailValueAttr;
	
	public TorRssHandler(Context context){
		DATA_TAG = "entry";
		idTag = context.getResources().getString(R.string.id);
		linkTag = context.getResources().getString(R.string.link);
		titleTag = context.getResources().getString(R.string.title);
		summaryTag = context.getResources().getString(R.string.summary);
		contentTag = context.getResources().getString(R.string.content);
		thumbnailTag = context.getResources().getString(R.string.thumbnail);
		thumbnailDataTypeAttr = context.getResources().getString(R.string.thumbnail_data_type);
		thumbnailValueAttr = context.getResources().getString(R.string.thumbnail_value);
		typeTag = context.getResources().getString(R.string.type);
		typeValueAttr = context.getResources().getString(R.string.type_value);
		lastUpdateTag = context.getResources().getString(R.string.last_update);
		authorTag = context.getResources().getString(R.string.author);
	}
	
	@Override
	public void startElement(String uri, String localName, String name, Attributes attributes)
		throws SAXException
	{
		super.startElement(uri, localName, name, attributes);
		if(this.currentItem != null){
			// Thumbnails are defined in tag attributes. 20110331LP
			if(localName.equalsIgnoreCase(thumbnailTag)){
				if("enclosure".equalsIgnoreCase(attributes.getValue(thumbnailDataTypeAttr)))
					currentItem.setThumbnail(attributes.getValue(thumbnailValueAttr));
				if(pd != null)
					pd.incrementProgressBy(1);
			// Categories are defined in tag attributes. 20110331LP
			}else if(localName.equalsIgnoreCase(typeTag)){
				currentItem.setType(attributes.getValue(typeValueAttr));
			}
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String name) throws SAXException
	{
		super.endElement(uri, localName, name);
		if(this.currentItem != null){
			// I'm not using else if's because some tags are used for multiple data items. LP
			if(localName.equalsIgnoreCase(idTag)){
				currentItem.setId(builder.toString());
			}
			if(localName.equalsIgnoreCase(linkTag)){
				currentItem.setUrl(builder.toString());
			}
			if(localName.equalsIgnoreCase(titleTag)){
				currentItem.setTitle(builder.toString());
			}
			if(localName.equalsIgnoreCase(summaryTag)){
				// Trim is in place because a newline was push content down a line. LP
				currentItem.setSummary(builder.toString().trim());
			}
			if(localName.equalsIgnoreCase(contentTag)){
				currentItem.setContent(builder.toString());
			}
			if(localName.equalsIgnoreCase(lastUpdateTag)){
//				currentItem.setLastUpdate(builder.toString());
				// TODO parse date
			}
			if(localName.equalsIgnoreCase(authorTag)){
				currentItem.setAuthor(builder.toString());
			}
			builder.setLength(0);
		}
	}
}
